package com.wxj.book.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxj.book.cms.dao.ContentMapper;
import com.wxj.book.cms.dao.ContentVoMapper;
import com.wxj.book.cms.pojo.Content;
import com.wxj.book.cms.service.ContentService;
import com.wxj.book.cms.vo.ContentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    private ContentMapper contentMapper;
    @Autowired
    private ContentVoMapper contentVoMapper;
    /**
     * 带有分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<ContentVo> getAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ContentVo> list = contentVoMapper.getAll();
        PageInfo<ContentVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 更新或者添加
     * @param content
     * @return
     */
    @Override
    public int update(Content content) {
        if (content.getId() == null) {
            Content newContent = new Content();
            newContent.setContent(content.getContent());
            newContent.setMajorId(content.getMajorId());
            newContent.setStatus(content.getStatus());
            return contentMapper.insertSelective(newContent);
        }
        Content newContent = new Content();
        newContent.setId(content.getId());
        newContent.setContent(content.getContent());
        newContent.setMajorId(content.getMajorId());
        newContent.setStatus(content.getStatus());
        return contentMapper.updateByPrimaryKeySelective(newContent);
    }

    /**
     * 根据id删除
     * @param contentId
     * @return
     */
    @Override
    public int deleteById(Integer contentId) {
        return contentMapper.deleteByPrimaryKey(contentId);
    }
}
